package com.yuyue.springFramework.note07.entity;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * 定义环境 bean 的条件类,此类中设置当前生效的环境
 */
public class EnvCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //获取当前设定的环境
        EnvConditional.ENV curEnv = EnvConditional.ENV.PROD;
        //获取 EnvConditional 类型注解的值
        EnvConditional.ENV env = (EnvConditional.ENV) metadata.getAllAnnotationAttributes(EnvConditional.class.getName()).get("value").get(0);
        //判断是否与设定一致
        return curEnv.equals(env);
    }
}
